//
//  BSSViewTool.m
//  BrightSunnySky
//
//  Created by purang on 2018/12/11.
//  Copyright © 2018年 purang. All rights reserved.
//

#import "BSSViewTool.h"


@implementation BSSViewTool

#pragma mark - 工厂创建控件

// 创建 label
+ (UILabel *)createLabelWithFrame:(CGRect)frame text:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor backgroundColor:(UIColor *)backgroundColor autoAdjustFontSize:(BOOL)adjust textAligenment:(NSTextAlignment)textAligenment {
    UILabel *label = [[UILabel alloc] init];
    label.frame = frame;
    if (text) {
        label.text = text;
    }
    if (font) {
        label.font = font;
    }
    if (textColor) {
        label.textColor = textColor;
    }
    label.backgroundColor = [UIColor whiteColor];
    if (backgroundColor) {
        label.backgroundColor = backgroundColor;
    }
    label.adjustsFontSizeToFitWidth = adjust;
    label.textAlignment = textAligenment;
    
    return label;
}

// 创建 button
+ (UIButton *)createButtonWithFrame:(CGRect)frame title:(NSString *)title titleFont:(UIFont *)titleFont titleColor:(UIColor *)titleColor normaleBackgroundImage:(UIImage *)normaleBackgroundImage highlightBackgroundImage:(UIImage *)highlightBackgroundImage normalImage:(UIImage *)normalImage highlightImage:(UIImage *)highlightImage backgroundColor:(UIColor *)backgroundColor actionTarget:(id)obj sel:(SEL)sel {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    if (title) {
        [button setTitle:title forState:UIControlStateNormal];
    }
    if (titleFont) {
        button.titleLabel.font = titleFont;
    }
    if (titleColor) {
        [button setTitleColor:titleColor forState:UIControlStateNormal];
    }
    if (normaleBackgroundImage) {
        [button setBackgroundImage:normaleBackgroundImage forState:UIControlStateNormal];
    }
    if (highlightBackgroundImage) {
        [button setBackgroundImage:highlightBackgroundImage forState:UIControlStateHighlighted];
    }
    if (normalImage) {
        [button setImage:normalImage forState:UIControlStateNormal];
    }
    if (highlightImage) {
        [button setImage:highlightImage forState:UIControlStateHighlighted];
    }
    if (backgroundColor) {
        [button setBackgroundColor:backgroundColor];
    }
    if (sel && obj) {
        [button addTarget:obj action:sel forControlEvents:UIControlEventTouchUpInside];
    }

    return button;
}

+ (UIView *)createViewWithFrame:(CGRect)frame backgroundColor:(UIColor *)backgroundColor {
    UIView *view = [[UIView alloc] initWithFrame:frame];
    if (backgroundColor) {
        view.backgroundColor = backgroundColor;
    }
    return view;
}

#pragma mark - 设置view的边框和圆角

+ (void)transferViewBorderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth cornerRadius:(CGFloat)cornerRadius view:(UIView *)view {
    if (![view isKindOfClass:[UIView class]]) {
        return;
    }
    
    if (cornerRadius >= 0) {
        view.layer.cornerRadius = cornerRadius;
    }
    
    if (borderWidth >= 0) {
        view.layer.borderWidth = borderWidth;
    }
    
    if (borderColor) {
        view.layer.borderColor = borderColor.CGColor;
    }
}

#pragma mark - 计算文本

// 计算文本的高度
+ (CGFloat)caculateStringHeight:(CGFloat)standardWidth text:(NSString *)text attributes:(NSDictionary *)attributes {
    CGFloat height = 0;
    if (![text isKindOfClass:[NSString class]]) {
        return height;
    }
    NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:text attributes:attributes];
    CGSize sizeToFit = [attrStr boundingRectWithSize:CGSizeMake(standardWidth, MAXFLOAT)
                                           options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                           context:nil].size;
    height = sizeToFit.height;
    return floor(height) + 1;
}

// 计算文本的宽度
+ (CGFloat)caculateStringWidth:(CGFloat)standardHight text:(NSString *)text attributes:(NSDictionary *)attributes {
    CGFloat width = 0;
    if (![text isKindOfClass:[NSString class]]) {
        return width;
    }
    NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:text attributes:attributes];
    CGSize sizeToFit = [attrStr boundingRectWithSize:CGSizeMake(MAXFLOAT, standardHight)
                                             options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                             context:nil].size;
    width = sizeToFit.width;
    return floor(width) + 1;
}

#pragma mark - 改变按钮图片  文字位置
//
+ (void)buttonImagePosition:(BSSViewToolButtonPosition)position button:(UIButton *)button space:(CGFloat)space {
    CGFloat imgWidth = button.imageView.bounds.size.width;
    CGFloat labWidth = button.titleLabel.bounds.size.width;
    CGFloat imgHeight = button.imageView.bounds.size.height;
    CGFloat labHeight = button.titleLabel.bounds.size.height;
    if (position == BSSViewToolButtonPositionTop) {
        [button setImageEdgeInsets:UIEdgeInsetsMake(-labHeight / 2.0 - space / 2.0, labWidth / 2.0, labHeight / 2.0 + space / 2.0, -labWidth / 2.0)];
        [button setTitleEdgeInsets:UIEdgeInsetsMake(imgHeight / 2.0 + space / 2.0, -imgWidth / 2.0, -imgHeight / 2.0 - space / 2.0, imgWidth / 2.0)];
    } else if (position == BSSViewToolButtonPositionBottom) {
        [button setImageEdgeInsets:UIEdgeInsetsMake(labHeight / 2.0 + space / 2.0, labWidth / 2.0, -labHeight / 2.0 - space / 2.0, -labWidth / 2.0)];
        [button setTitleEdgeInsets:UIEdgeInsetsMake(-imgHeight / 2.0 - space / 2.0, -imgWidth / 2.0, imgHeight / 2.0 + space / 2.0, imgWidth / 2.0)];
    } else if (position == BSSViewToolButtonPositionRight) {
        [button setImageEdgeInsets:UIEdgeInsetsMake(0, labWidth + space / 2.0, 0, -labWidth - space / 2.0)];
        [button setTitleEdgeInsets:UIEdgeInsetsMake(0, -imgWidth - space / 2.0, 0, imgWidth + space / 2.0)];
    }  else if (position == BSSViewToolButtonPositionLeft) {
        [button setImageEdgeInsets:UIEdgeInsetsMake(0, - space / 2.0, 0, space / 2.0)];
        [button setTitleEdgeInsets:UIEdgeInsetsMake(0, + space / 2.0, 0, - space / 2.0)];
    }
}

+ (void)rectCorners:(UIRectCorner)corner cornerRadii:(CGSize)radii view:(UIView *)view {
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:corner cornerRadii:radii].CGPath;
    view.layer.mask = shapeLayer;
}

@end
